package com.yanqu.road.server.cmd.activity.commonwish;

import com.yanqu.road.entity.activity.commonwish.CommonWishConfig;
import com.yanqu.road.entity.activity.commonwish.CommonWishUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CommonWishProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.commonwish.CommonWishModule;
import com.yanqu.road.server.gameplayer.module.activity.commonwish.CommonWishPb;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.commonwish.CommonWishMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@CmdApi(reqMsg = CommonWishProto.CommonWishGetUserDataReqMsg.class, respMsg = CommonWishProto.CommonWishGetUserDataRespMsg.class)
@Cmd(code = Protocol.S_COMMON_WISH_GET_USER_DATA, desc = "获取玩家数据")
public class CommonWishGetUserDataCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        CommonWishProto.CommonWishGetUserDataReqMsg reqMsg = CommonWishProto.CommonWishGetUserDataReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        CommonWishProto.CommonWishGetUserDataRespMsg.Builder syncMsg = CommonWishProto.CommonWishGetUserDataRespMsg.newBuilder();
        syncMsg.setRet(0);
        Map<Integer, CommonWishUserData> userDataMap = player.getModule(CommonWishModule.class).getUserDataMap();
        for (Map.Entry<Integer, CommonWishConfig> entry : CommonWishMgr.getConfigMap().entrySet()) {

            if(entry.getKey() != activityId && activityId > 0){
                continue;
            }
            CommonWishConfig config = entry.getValue();

            if (!ActivityMgr.activityInShowTime(config.getActivityInfo())) {
                continue;
            }

            CommonWishUserData userData = userDataMap.get(config.getActivityInfo().getActivityId());
            if(userData == null){
                continue;
            }

            CommonWishProto.CommonWishUserDataTemp.Builder builder = CommonWishPb.buildUserData(userData);
            syncMsg.addUserData(builder);
        }

        player.sendPacket(Protocol.U_COMMON_WISH_GET_USER_DATA, syncMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
